package org.red_cross_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.red_cross_service.common.response.Response;
import org.red_cross_service.domain.entity.SysUserEntity;
import org.red_cross_service.mapper.SysUserMapper;
import org.red_cross_service.utils.DigestUtil;
import org.red_cross_service.utils.QueryWrapperUtils;
import org.red_cross_service.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TokenUtil tokenUtil;



    public Response login(String username, String password) {
        SysUserEntity oldSysUser = sysUserMapper.selectOne(
                new QueryWrapper<SysUserEntity>()
                        .eq("username", username)
                        .eq("password", DigestUtil.encryptPassword(password))
        );
        if(oldSysUser==null){
            return Response.error("用户名或密码错误");
        }
        oldSysUser.setPassword(null);
        String token = tokenUtil.createToken(oldSysUser.getId().toString());
        oldSysUser.setToken(token);
        return Response.success(oldSysUser);
    }

    public Response add(SysUserEntity sysUserEntity) {
        sysUserEntity.setPassword(DigestUtil.encryptPassword(sysUserEntity.getPassword()));
        return sysUserMapper.insert(sysUserEntity) > 0 ? Response.success() : Response.error();
    }
    public Response update(SysUserEntity sysUserEntity) {
        return sysUserMapper.updateById(sysUserEntity) > 0 ? Response.success() : Response.error();
    }
    public Response delete(Long id) {
        return sysUserMapper.deleteById(id) > 0 ? Response.success() : Response.error();
    }
    public Response selectOne(SysUserEntity sysUserEntity) {
        return Response.success(sysUserMapper.selectOne(
                new QueryWrapper<SysUserEntity>()
                        .eq("username", sysUserEntity.getUsername())
                        .eq("password", DigestUtil.encryptPassword(sysUserEntity.getPassword()))
        ));
    }

    public Response getSysUserList(SysUserEntity sysUserEntity) {
        return Response.success(sysUserMapper.selectList(
                QueryWrapperUtils.buildQuery(
                        sysUserEntity
                )
        ));
    }

    public int updatePassword(SysUserEntity sysUserEntity) {
             if (sysUserMapper.selectOne(
                new QueryWrapper<SysUserEntity>()
                        .eq("username", sysUserEntity.getUsername())
                        .eq("password", DigestUtil.encryptPassword(sysUserEntity.getPassword()))
                        )==null){
            return 0;
        }
             sysUserEntity.setPassword(DigestUtil.encryptPassword(sysUserEntity.getNewPassword()));
        return sysUserMapper.update(
                sysUserEntity,
                new QueryWrapper<SysUserEntity>()
                        .eq("id", sysUserEntity.getId())
                        .eq("username", sysUserEntity.getUsername())
        );
    }
}
